
var express = require('express')
var path = require('path')
const app = express()
const {createServer} = require("http");
const rootPath = path.join(__dirname, './');
let server = null;
function judgeServerOpen(port){
    let order = `netstat -ano|findstr "${port}"`;
    return new Promise((resolve, reject) => {
      cProcess.exec(order, function (error,stdout,stderr) {
        if (stdout === "") {
          resolve(false);
        } else {
          resolve(true);
        }
      });
    })
  }
app.use(express.static(rootPath));
module.exports = {
    startServer(){
        return new Promise((resolve, reject)=>{
                server = createServer(app);
                server.listen(3000);
                server.on('error', (error) => {
                    switch (error.code) {
                      case 'EACCES':
                        reject('权限不足内置服务器启动失败，请使用管理员权限运行。')
                        break
                      case 'EADDRINUSE':
                        reject('内置服务器端口已被占用，请检查。')
                        break
                      default:
                        reject(error)
                    }
                  })
                  server.on('listening', () => {
                    resolve('服务端运行中')
                  })
        })
        
    },
    StopServer() {
        return new Promise((resolve, reject) => {
          if (server) {
            server.close()
            server.on('close', () => {
              server = null
              resolve(1)
            })
          } else {
            reject('服务端尚未开启')
          }
        })
      }
}
